function [ID,MosMom,tsOut] = OffsetsfromCell(StatName,ts_or_OptsGen,OffsetsCell)
% OffsetsfromCell - Steps from a database of a GNSS station
%
%   [ID,MosMom,tsOut] = OffsetsfromCell(StatName,ts_or_OptsGen,OffsetsCell)
% 
% This function provides, for the station StatName, the offset matrix MosMom
% from the general offsets cell variable OffetsCell. The output argument ID
% is true if at least an offset is found, false elsewere. 
% Options about the second input argument ts_or_OptsGen: 
%   - If it is a tsData object, the function simply provides, besided ID 
%     and MosMom, the output tsData object tsOut;
%   - If the second input argument is an OptsGen "object-style" parameter 
%     cell variable, the corresponding tsData object file is loaded and 
%     modified and mom file(s) is(are) are generated and placed in the 
%     folder OptsGen.dirObs. 
% The last input argument is the N-by-3 cell variable whose columns are
%       {StatName, Date, typeEvent},
% where 
%   StatName(k) is the station name related to the k-th event
%   Date(k) is the corresponding date (convention: YMD);
%   typeEvent(k) is 1 in the case of change antenna/receiver/other changes, 
%   and 2 in the case of a relatively near earthquake.
%
% See also NevadaAllOffsets, MosMom2MosNeu.

% G. Teza, 2022

if isa(ts_or_OptsGen,'tsData')
    ts = ts_or_OptsGen;
    OptsGen = [];
else
    OptsGen = ts_or_OptsGen;
    fileTs = fullfile(OptsGen.dirTs,[StatName OptsGen.AddTs '.mat']);
    ts = tsDataFileIn(fileTs);
end
if isempty(ts)
    ID = false;
    MosMom = [];
    tsOut = [];
    return
end

I = find(contains(OffsetsCell(:,1),StatName));
if isempty(I)
    ID = false;
    MosMom = [];
    tsOut = ts;
    if isempty(OptsGen) 
        return
    else
        ICV = OptsGen.components;
    end
else
    ID = true;
    NI = numel(I);
    dateYMD = repmat(' ',NI,7);
    for k = 1:NI
        datek = OffsetsCell{I(k),2};
        dateYMD(k,:) = datek;
    end
    dateN = YMD2serial(dateYMD);
    dateN = sort(dateN);
    dateN = serial2MJD(dateN); 
    MosMom = zeros(NI,2);
    MosMom(:,1) = dateN;
    if isempty(OptsGen)
        ICV = 0:2;
        MosMom(:,2) = 7;
    else
        ICV = OptsGen.components;
        if numel(ICV) == 3
            MosMom(:,2) = 7;
        elseif numel(ICV) == 2
            MosMom(:,2) = 6;
        elseif numel(ICV) == 1
            MosMom(:,2) = 1;
        end
    end
end

tsOut = ts;

if ~isempty(MosMom)
    if numel(ICV) == 3
        tsOut.offsetsE = MJD2serial(MosMom(:,1));
        tsOut.offsetsN = MJD2serial(MosMom(:,1));
        tsOut.offsetsV = MJD2serial(MosMom(:,1));
    elseif numel(ICV) == 2
        tsOut.offsetsE = MJD2serial(MosMom(:,1));
        tsOut.offsetsN = MJD2serial(MosMom(:,1));
        tsOut.offsetsV = [];
    elseif numel(ICV) == 1
        tsOut.offsetsE = [];
        tsOut.offsetsN = [];
        tsOut.offsetsV = MJD2serial(MosMom(:,1));
    end
end

MosNeu = MosMom2MosNeu(MosMom);

if ~isempty(OptsGen)
    ts = tsOut;
    save(fileTs,'ts');
    writeNeuMom(fullfile(OptsGen.dirObs,[StatName OptsGen.AddObs]),...
        ICV,ts,MosNeu);
end